自己从源码编译vim

发布于 2017-11-09 15:12:07

闲来无事折腾下vim。

vim有很多版本,常见的是 vim-tiny, vim-nox, vim-gtk, vim-gnome。

我在想编译完成的vim,能否直接复制到其他机器运行呢?答案是不行,软件依赖的库有多种,分为头文件、静态文件、动态文件。头文件在编译阶段使用,静态文件在连接阶段使用,而动态文件在真正运行阶段使用。

动态文件提高了库的复用度,减小了软件体积,却带来了移植运行的不方便,因为需要安装动态库。

最近一直在用的语言是 Python、Go、Rust,JS也会用,haskell可能有空的时候用,常见的ruby倒是没用过,虽然社区氛围很好,却没有使用的欲望。

想要vim完美支持开发的话,需要代码高亮、代码补全、自动缩进、错误提示、代码跳转,暂时就想到这些。

大名鼎鼎的代码补全 YouCompleteMe 虽然体积很大,曾经也安装失败过(当然这与中国网络也有一定因素),但还是选择他作为补全工具吧。

编译vim

参考了以下链接:

下面简单记录下在 Debian8(相当于ubuntu14.04?)环境下的安装过程,其他系统差异还是参考原文吧。

vutlr和digitalocean都提供Ubuntu16.04发行版,就以这个在vutlr新开一台机器试验。

安装头文件依赖

sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \
    libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
    libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
    python3-dev ruby-dev lua5.1 liblua5.1-dev libperl-dev git

On Ubuntu 16.04, liblua5.1-dev is the lua dev package name not lua5.1-dev.

其中python-dev, ruby-dev是可选的,按照自己使用语言情况决定。git 是用来克隆代码库,网络原因可以用 wget 下下来。

卸载现有vim

列出已安装的

root@vultr:~# dpkg -l | grep vim
ii  vim                                2:7.4.1689-3ubuntu1.2                      amd64        Vi IMproved - enhanced vi editor
ii  vim-common                         2:7.4.1689-3ubuntu1.2                      amd64        Vi IMproved - Common files
ii  vim-runtime                        2:7.4.1689-3ubuntu1.2                      all          Vi IMproved - Runtime files
ii  vim-tiny                           2:7.4.1689-3ubuntu1.2                      amd64        Vi IMproved - enhanced vi editor - compact version

全部卸载

root@vultr:~# apt-get remove vim vim-common vim-runtime vim-tiny

下载源码

root@vultr:~# curl -sSL https://github.com/vim/vim/archive/master.zip > vim.zip
root@vultr:~# apt install unzip
root@vultr:~# unzip -q vim.zip

配置

Ubuntu16.04的自带Python版本是 python2.7.12 和 python3.5.2

Also, if you’re not using vim 8.0, make sure to set the VIMRUNTIMEDIR variable correctly

./configure --prefix=/usr/local \
            --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/usr/lib/python2.7/config-x86_64-linux-gnu/ \
            --enable-python3interp=yes \
            --with-python3-config-dir=/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/ \
            --enable-cscope \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2

以上的perl、lua、gtk2支持是不必要的,在终端环境的话,可以去掉,除非第三方扩展使用了perl、lua代码。

报错的话需要安装编译工具

apt-get install build-esstential cmake

编译、安装

make && make install

安装完成!查看版本信息:

root@vultr:~/vim-master# vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov  9 2017 08:24:48)
Included patches: 1-1274
Compiled by root@vultr.guest
Huge version with GTK2 GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     -tcl
+balloon_eval    +folding         +mouse_xterm     +termguicolors
+browse          -footer          +multi_byte      +terminal
++builtin_terms  +fork()          +multi_lang      +terminfo
+byte_offset     -gettext         -mzscheme        +termresponse
+channel         -hangul_input    +netbeans_intg   +textobjects
+cindent         +iconv           +num64           +timers
+clientserver    +insert_expand   +packages        +title
+clipboard       +job             +path_extra      +toolbar
+cmdline_compl   +jumplist        +perl            +user_commands
+cmdline_hist    +keymap          +persistent_undo +vertsplit
+cmdline_info    +lambda          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      +python3/dyn     +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con_gui  +lua             +rightleft       +windows
+diff            +menu            +ruby            +writebackup
+digraphs        +mksession       +scrollbind      +X11
+dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     +xim
+emacs_tags      +mouseshape      +startuptime     +xpm
+eval            +mouse_dec       +statusline      +xsmp_interact
+ex_extra        -mouse_gpm       -sun_workshop    +xterm_clipboard
+extra_search    -mouse_jsbterm   +syntax          -xterm_save
+farsi           +mouse_netterm   +tag_binary
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2   -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E   -L/usr/local/lib -Wl,--as-needed -o vim   -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lnsl  -lselinux   -ldl  -L/usr/lib -llua5.1 -Wl,-E  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/x86_64-linux-gnu/perl/5.22/CORE -lperl -ldl -lm -lpthread -lcrypt    -lruby-2.3 -lpthread -lgmp -ldl -lcrypt -lm

设置默认编辑器

# sudo update-alternatives --install /usr/bin/editor editor `which vim` 1
# sudo update-alternatives --set editor `which vim`
update-alternatives: using /usr/local/bin/vim to provide /usr/bin/editor (editor) in manual mode
# sudo update-alternatives --install /usr/bin/vi vi `which vim` 1
update-alternatives: using /usr/local/bin/vim to provide /usr/bin/vi (vi) in auto mode
# sudo update-alternatives --set vi `which vim`

安装YouCompleteMe

  1. 安装vundle插件管理工具
  2. 安装YCM

See YouCompleteMe and YouCompleteMe, Rust

YCM编译

cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive

./install.py --gocode-completer --racer-completer
# or ensure xbuild, go, tsserver, node, npm, rustc, and cargo tools are installed and in your PATH, then:
./install.py --all

为Python配置开发环境

为Golang配置开发环境

install golang

curl -O https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz
tar xf go*.tar.gz && sudo mv go /usr/local
# then setup env variable

为Rust配置开发环境

1.Install rust

curl https://sh.rustup.rs -sSf | sh
# then setup env variable

2.Download rust source

git clone --depth 1 --branch master https://github.com/rust-lang/rust ~/.cargo/rust-master

3.Set let g:ycm_rust_src_path="~/.cargo/rust-master/src" for YCM.

4.(optional?) Install racer

cargo install racer
# then install `vim-racer` plugin for VIM

最后配置:

Plugin 'Valloric/YouCompleteMe'
let g:ycm_rust_src_path = '~/.cargo/rust-master/src'

Plugin 'racer-rust/vim-racer'
set hidden
let g:racer_cmd = "~/.cargo/bin/racer"
let g:racer_experimental_completer = 1
au FileType rust nmap gd <Plug>(rust-def)
au FileType rust nmap gs <Plug>(rust-def-split)
au FileType rust nmap gx <Plug>(rust-def-vertical)
au FileType rust nmap <leader>gd <Plug>(rust-doc)

racerd is a Rust semantic analysis server powered by Racer

为JavaScript配置开发环境

使用VIM需要知道技巧

  • tag跳转:vim 默认是集成了 ctags 的接口,使用 ctags 产生 tag 文件后. ctrl+[ 可跳转到函数的定义与声明.使用 ctrl+o 可以跳回上一个缓冲区, ctrl+i 可以跳到下一个缓冲区.这三个快捷键基本上可以满足你到处跳来跳去了.
  • register:命名的粘贴板
  • macro:宏录制来执行重复操作